<?php
namespace app\admin\controller;

use think\Controller;
vendor('qiniu/php-sdk/autoload.php');
use Qiniu\Auth;
use Qiniu\Storage\UploadManager; //实例化上传类
use think\Db;
use think\Request;
use think\session;
use Qiniu\Processing\PersistentFop;

class Common extends Controller
{
    protected $page = 1;   // 第几页
    protected $size = 10;  // 每页显示的条目数
    protected $from = 0;   // 从第几条开始
    public function _initialize()
    {
        $uid = session('admin_id');

        if(empty($uid)) {
            return $this->redirect('login/login');
        }

    }
    /**
     * @param $data  从第几条开始，每页显示的条目数
     */
    // 分页设置
    public function getPageAndSize($data)
    {
        $this->page = !empty($data['page']) ? $data['page'] : 1;
        // 每页显示条数
        $this->size = !empty($data['limit']) ? $data['limit'] : config('config.page_size');
        // 页数小于0显示第一页内容
        if ($this->page <= 0) {
            $this->page = 1;
        }
        $this->from = ($this->page - 1) * $this->size;
    }
    //没有水印
    public function upload(Request $request)
    {
        $file = request()->file('file');
        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀
        // 上传到七牛后保存的文件名
        $key ='ysly/'.substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
        // 需要填写你的 Access Key 和 Secret Key
        // 构建鉴权对象
        $accessKey =config("qiniu")["accessKey"];
        $secretKey =config("qiniu")["secretKey"];
        $auth=new Auth($accessKey,$secretKey);
        // 要上传的空间
        $bucket =config("qiniu")["bucket"];
        //域名
        $domain=config("qiniu")["domain"];
        $token = $auth->uploadToken($bucket);
        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        if ($err !== null) {
            return ["code"=>0,"msg"=>$err,"data"=>""];
        } else {
            //返回图片的完整URL
            $imgPath=$domain.'/'.$key;

            return ["code"=>1,"msg"=>"上传成功","data"=>$imgPath];
        }

    }
    //有水印
    public function have_upload()
    {
         $file = request()->file('file');
        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀
        // 上传到七牛后保存的文件名
        $key ='ysly/'.substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
        // 需要填写你的 Access Key 和 Secret Key
        // 构建鉴权对象
        $accessKey =config("qiniu")["accessKey"];
        $secretKey =config("qiniu")["secretKey"];
        $auth=new Auth($accessKey,$secretKey);
        // 要上传的空间
        $bucket =config("qiniu")["bucket"];
        //域名
        $domain=config("qiniu")["domain"];
        $token = $auth->uploadToken($bucket);
        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        if ($err !== null) {
            return ["code"=>0,"msg"=>$err,"data"=>""];
        } else {
            //返回图片的完整URL
            $imgPath=$domain.'/'.$key.'-lv';

            return ["code"=>1,"msg"=>"上传成功","data"=>$imgPath];
        }
    }
    //检查用户权限
    protected function checkAuth($action){
        $admin_id = session('admin_id');
        $Auth= new \Auth();
        if ($admin_id != 1){
            if($Auth->check($action,$admin_id)) {
                return 1;
            }else{
                return 2;
            }
        }else{
            return 1;
        }

    }

    public function uploadOneFileToQiniu($prefix = 'lv/hotel/'){

        $file = request()->file('file');
        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀
        // 上传到七牛后保存的文件名
        $key = $prefix . substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;

        // 需要填写你的 Access Key 和 Secret Key
        // 构建鉴权对象
        $accessKey = config("qiniu")["accessKey"];
        $secretKey = config("qiniu")["secretKey"];

        $auth = new Auth($accessKey, $secretKey);
        // 要上传的空间
        $bucket = config("qiniu")["bucket"];
        //域名
        $domain = config("qiniu")["domain"];
        $token = $auth->uploadToken($bucket);

        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        if ($err !== null) {
            return ["code" => 1, "msg" => $err, "data" => ""];
        } else {
            //返回图片的完整URL
            $imgPath = $domain . '/' . $key;

            return ["code" => 0, "msg" => "上传成功", "data" => $imgPath];
        }

    }
}